<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">

    <body>

        <ui:composition template="./TemplatePrincipal.xhtml">

            <ui:define name="top">
                <h:form>
                    <p:tabMenu id="menu" activeIndex="4" >  
                        <p:menuitem value="Home" action="MenuPrincipal" />  
                        <p:menuitem value="Users" action="UsuarioFacelets"/>  
                        <p:menuitem value="Author" action="AuthorFacelets" /> 
                        <p:menuitem value="Category" action="CategoryFacelets" />
                        <p:menuitem value="Book" action="BookFacelets" />
                    </p:tabMenu> 
                </h:form>  
            </ui:define>

            <ui:define name="left">
                left
            </ui:define>

            <ui:define name="content">
                content
                <f:view>
                    <h:form id="formBook">
                        <p:panel id="panelBook" visible="false">
                            <f:facet name="options">  
                                <p:menu>  
                                </p:menu>  
                            </f:facet> 
                        </p:panel>

                        <p:panel  id="panelRegistro" header="New Book" toggleable="true" toggleOrientation="horizontal" collapsed="true" 
                                  widgetVar="varBook" style="text-align-last: justify">  
                            <p:growl id="growlBook" showDetail="true"/> 

                            <h:panelGrid columns="2"  style="margin-bottom:10px">
                                <p:outputLabel value="Clave:" for="clave" />
                                <p:inputText id="clave" value="#{bookBean.book.clave}" title="Clave" required="true" requiredMessage="The Clave field is required."/>
                                <p:outputLabel value="Nombre:" for="nombre" />
                                <p:inputText id="nombre" value="#{bookBean.book.nombre}" title="Nombre" required="true" requiredMessage="The Nombre field is required."/>
                                <p:outputLabel value="Isbn:" for="isbn" />
                                <p:inputText id="isbn" value="#{bookBean.book.isbn}" title="Isbn" required="true" requiredMessage="The Isbn field is required."/>
                                <p:outputLabel value="FechaRegistro:" for="fechaRegistro" />
                                <p:calendar id="fechaRegistro" value="#{bookBean.book.fechaRegistro}" pattern="dd/MM/yyyy" effect="explode" title="FechaRegistro" required="true" requiredMessage="The FechaRegistro field is required.">
                                </p:calendar>
                                <p:outputLabel value="Paginas:" for="paginas" />
                                <p:inputText id="paginas" value="#{bookBean.book.paginas}" title="Paginas" />
                                <p:outputLabel value="Estado:" for="estado" />
                                <p:inputText id="estado" value="#{bookBean.book.estado}" title="Estado" required="true" requiredMessage="The Estado field is required."/>
                                <p:outputLabel value="Disponible:" for="disponible" />
                                <p:selectOneRadio id="disponible" value="#{bookBean.book.disponible}" required="true" requiredMessage="The Disponible field is required.">
                                    <f:selectItem itemLabel="false" itemValue="false" />  
                                    <f:selectItem itemLabel="true" itemValue="true" /> 
                                </p:selectOneRadio>
                                <p:outputLabel value="IdAutor:" for="idAutor" />
                                <p:selectOneMenu id="idAutor" value="#{bookBean.idAuthor}"  panelStyle="width:160px"  effect="fade" var="p" style="width:160px"  
                                                 filter="true" filterMatchMode="startsWith"
                                                 required="true" requiredMessage="The IdAutor field is required.">
                                    <!-- TODO: update below reference to list of available items-->
                                    <f:selectItem itemLabel="Select One" itemValue=""/> 
                                    <f:selectItems value="#{bookBean.listAutor}" var="h" itemLabel="#{h.nombre} #{h.apellidoPaterno} #{h.apellidoMaterno}"/>
                                    <p:column> 
                                        #{p.nombre} #{p.apellidoPaterno} #{p.apellidoMaterno}
                                    </p:column>  

                                </p:selectOneMenu>
                                <p:outputLabel value="Categoria:" for="categorias" />
                                <p:selectCheckboxMenu id="categorias" value="#{bookBean.categoriasSelect}" label="Movies" filter="true" filterMatchMode="startsWith" panelStyle="width:220px">  
                                    <f:selectItems value="#{bookBean.listCategorias}" var="aut" itemLabel="#{aut.clave}"/>  
                                </p:selectCheckboxMenu>  



                                <p:commandButton id="button" value="Guardar" update="formBook:growlBook :formBook:panelRegistro :formBook:displayBook"
                                                 oncomplete="if(!args.validationFailed){bookDialog.show()}"/>
                                <p:commandButton  value="Cancelar" onclick="varBook.toggle()" update="formBook:panelRegistro" immediate="true" /> 

                            </h:panelGrid>

                            <p:ajax event="toggle" listener="#{bookBean.handleToggle}" update=":formBook:growlBook :formBook:panelRegistro :formBook:panelBook"  process="@this"/>  

                        </p:panel>


                        <p:dialog id="jjjj" header="Selected Movies" modal="true" showEffect="fade" hideEffect="fade" widgetVar="dlg" resizable="false">  
                            <h:panelGroup id="displayList">
                                <p:dataList id="list" value="#{bookBean.categoriasSelect}" var="movie" >  
                                    #{movie}  
                                </p:dataList>  
                            </h:panelGroup>
                        </p:dialog>  

                        <p:dialog id="displayBook" header="Confirm Book" widgetVar="bookDialog" resizable="false"  
                                  showEffect="fade" hideEffect="explode">  
                            <h:panelGrid id="displa" columns="2" cellpadding="4">  
                                <h:panelGrid id="r" columns="2" >  
                                    <h:outputText value="Clave:"/>
                                    <h:outputText value="#{bookBean.book.clave}" title="Clave"/>
                                    <h:outputText value="Nombre:"/>
                                    <h:outputText value="#{bookBean.book.nombre}" title="Nombre"/>
                                    <h:outputText value="Isbn:"/>
                                    <h:outputText value="#{bookBean.book.isbn}" title="Isbn"/>
                                    <h:outputText value="FechaRegistro:"/>
                                    <h:outputText value="#{bookBean.book.fechaRegistro}" title="FechaRegistro">
                                        <f:convertDateTime pattern="MM/dd/yyyy" />
                                    </h:outputText>
                                    <h:outputText value="Paginas:"/>
                                    <h:outputText value="#{bookBean.book.paginas}" title="Paginas"/>
                                    <h:outputText value="Estado:"/>
                                    <h:outputText value="#{bookBean.book.estado}" title="Estado"/>
                                    <h:outputText value="Disponible:"/>
                                    <h:outputText value="#{bookBean.book.disponible}" title="Disponible"/>
                                    <h:outputText value="IdAutor:"/>
                                    <h:outputText value="#{bookBean.idAuthor}" title="IdAutor"/>
                                    <p:commandButton value="GuardarCambios"  action="#{bookBean.crearBook()}" onclick="bookDialog.hiden()" />
                                    <p:commandButton value="Cancelar" onclick="bookDialog.hiden()" update="panelRegistro" immediate="true" /> 
                                </h:panelGrid> 
                            </h:panelGrid>
                        </p:dialog> 
                    </h:form>
                </f:view>

                <f:view id="viewTableBook">
                    <h:form id="formTableBook" >
                        <p:growl id="growTableBook" showDetail="true"/>  
                        <p:dataTable value="#{bookBean.list}" var="item" widgetVar="bookVar" rowKey="#{item.id}"   emptyMessage="No Books found with given criteria" 
                                     filteredValue="#{bookBean.filteredBook}" selection="#{bookBean.book}" selectionMode="single" >

                            <p:ajax event="rowSelect" listener="#{bookBean.onEdit}" update=":formTableBook:displayB :formTableBook:growTableBook" 
                                    oncomplete="bookDialog.show()" /> 
                            <p:ajax event="rowUnselect" listener="#{bookBean.onRowUnselect}" update=":formTableBook:growTableBook"/>  

                            <f:facet name="header"> 
                                <p:outputPanel>  
                                    <h:outputText value="Search all fields:" />  
                                    <p:inputText id="globalFilter" onkeyup="bookVar.filter()" style="width:150px" />  
                                </p:outputPanel>  
                            </f:facet>

                            <p:column filterStyle="display:none" filterBy="#{item.clave}">
                                <f:facet name="header">
                                    <h:outputText value="Clave"/>
                                </f:facet>
                                <h:outputText value="#{item.clave}"/>
                            </p:column>
                            <p:column filterStyle="display:none" filterBy="#{item.nombre}">
                                <f:facet name="header">
                                    <h:outputText value="Nombre"/>
                                </f:facet>
                                <h:outputText value="#{item.nombre}"/>
                            </p:column>
                            <p:column filterStyle="display:none" filterBy="#{item.isbn}">
                                <f:facet name="header">
                                    <h:outputText value="Isbn"/>
                                </f:facet>
                                <h:outputText value="#{item.isbn}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="FechaRegistro"/>
                                </f:facet>
                                <h:outputText value="#{item.fechaRegistro}">
                                    <f:convertDateTime pattern="MM/dd/yyyy" />
                                </h:outputText>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Paginas"/>
                                </f:facet>
                                <h:outputText value="#{item.paginas}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Estado"/>
                                </f:facet>
                                <h:outputText value="#{item.estado}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Disponible"/>
                                </f:facet>
                                <h:outputText value="#{item.disponible}"/>
                            </p:column>
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="IdAutor"/>
                                </f:facet>
                                <h:outputText value="#{item.idAutor}"/>
                            </p:column>
                        </p:dataTable>
                        <p:dialog id="dialogBook" header="Book Detail" widgetVar="bookDialog" resizable="false"  
                                  showEffect="fade" hideEffect="explode">  

                            <h:panelGrid id="displayB" columns="2" cellpadding="4">  
                                <h:panelGrid id="d" columns="2">

                                    <h:outputLabel value="Clave:" for="clave" />
                                    <p:inplace id="ajaxInplace" editor="true" >
                                        <p:ajax event="save" listener="#{bookBean.handleSave()}" />
                                        <h:inputText id="clave" value="#{bookBean.book.clave}" title="Clave" required="true" requiredMessage="The Clave field is required."/>
                                    </p:inplace>
                                    <h:outputLabel value="Nombre:" for="nombre" />
                                    <p:inplace id="ajaxInplac" editor="true" >
                                        <p:ajax event="save" listener="#{bookBean.handleSave()}" update="formTableBook"/>
                                        <h:inputText id="nombre" value="#{bookBean.book.nombre}" title="Nombre" required="true" requiredMessage="The Nombre field is required."/>
                                    </p:inplace>
                                    <h:outputLabel value="Isbn:" for="isbn" />
                                    <p:inplace id="ajaxInpla" editor="true">
                                        <p:ajax event="save" listener="#{bookBean.handleSave()}" update="formTableBook"/>
                                        <h:inputText id="isbn" value="#{bookBean.book.isbn}" title="Isbn" required="true" requiredMessage="The Isbn field is required."/>
                                    </p:inplace>
                                    <h:outputLabel value="FechaRegistro:" for="fechaRegistro" />
                                    <p:inplace id="ajaxInpl" editor="true">
                                        <p:ajax event="save" listener="#{bookBean.handleSave()}" update="formTableBook"/>
                                        <p:calendar id="fechaRegistro" value="#{bookBean.book.fechaRegistro}" pattern="dd/MM/yyyy" effect="explode" title="FechaRegistro" required="true" requiredMessage="The FechaRegistro field is required.">
                                        </p:calendar>
                                    </p:inplace>
                                    <h:outputLabel value="Paginas:" for="paginas" />
                                    <p:inplace id="ajaxInp" editor="true">
                                        <h:inputText id="paginas" value="#{bookBean.book.paginas}" title="Paginas" />
                                    </p:inplace>
                                    <h:outputLabel value="Estado:" for="estado" />
                                    <p:inplace id="ajaxIn" editor="true">
                                        <h:inputText id="estado" value="#{bookBean.book.estado}" title="Estado" required="true" requiredMessage="The Estado field is required."/>
                                    </p:inplace>
                                    <h:outputLabel value="Disponible:" for="disponible" />
                                    <p:inplace id="ajaxI" editor="true">
                                        <h:inputText id="disponible" value="#{bookBean.book.disponible}" title="Disponible" required="true" requiredMessage="The Disponible field is required."/>
                                    </p:inplace>
                                    <h:outputLabel value="IdAutor:" for="idAutor" />
                                    <p:inplace id="ajax" editor="true">
                                        <p:ajax event="save" listener="#{bookBean.handleSave()}" update="formTableBook"/>
                                        <p:selectOneMenu id="idAutor" value="#{bookBean.idAuthor}"  panelStyle="width:160px"  effect="fade" var="p" style="width:160px"  
                                                         filter="true" filterMatchMode="startsWith"
                                                         required="true" requiredMessage="The IdAutor field is required.">
                                            <!-- TODO: update below reference to list of available items-->
                                            <f:selectItem itemLabel="Select One" itemValue=""/> 
                                            <f:selectItems value="#{bookBean.listAutor}" var="h" itemLabel="#{h.nombre} #{h.apellidoPaterno} #{h.apellidoMaterno}" />
                                            <p:column> 
                                                #{p.nombre} #{p.apellidoPaterno} #{p.apellidoMaterno}
                                            </p:column>  

                                        </p:selectOneMenu>
                                    </p:inplace>
                                    <p:outputLabel value="Categoria:" for="categorias" />
                                    <p:inplace id="ija" editor="true">
                                        <p:ajax event="save" listener="#{bookBean.handleSave()}" update="formTableBook"/>
                                        <p:selectCheckboxMenu id="categorias" value="#{bookBean.categoriasSelect}" label="Movies" filter="true" filterMatchMode="startsWith" panelStyle="width:220px">  
                                            <f:selectItems value="#{bookBean.listCategorias}" var="aut" itemLabel="#{aut.clave}"/>  
                                        </p:selectCheckboxMenu> 
                                    </p:inplace>
                                </h:panelGrid>
                            </h:panelGrid>  
                        </p:dialog> 
                    </h:form>
                </f:view>
            </ui:define>

        </ui:composition>

    </body>
</html>
